﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Mini.Common;
using Mini.Model;

namespace Mini.DownloadTool
{
    public partial class MainForm
    {
        private void GridRegister()
        {
            DgvListPageInitialize();

            DvgListLinkInitialize();
        }
        #region gridView Page list

        private void DgvListPageInitialize()
        {
            dgvListPage.AutoGenerateColumns = false;
            dgvListPage.CellClick += dgvListPage_CellClick;
            _pageListBindingSource = new BindingList<PageModel>();
            dgvListPage.DataSource = _pageListBindingSource;
            dgvListPage.CellFormatting += dgvListPage_CellFormatting;
        }
        private void dgvListPage_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not in our 
            var dataGridViewColumn = dgvListPage.Columns["btnDelete"];
            if (dataGridViewColumn != null && (e.ColumnIndex == dataGridViewColumn.Index && e.RowIndex >= 0))
            {
                var rowIndex = e.RowIndex;
                _pageListBindingSource.RemoveAt(rowIndex);
                ListPageRefresh();
            }
        }
        private void ListPageRefresh()
        {
            dgvListPage.Refresh();
        }
        private void dgvListPage_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            int colIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;

            if (rowIndex >= 0 && colIndex >= 0)
            {
                var theRow = dgvListPage.Rows[rowIndex];
                var statusCell = theRow.Cells["txtPageStatus"].Value.ToString();
                var cellPageStatus = statusCell.ConvertValueToEnum<PageStatus>();
                switch (cellPageStatus)
                {
                    case PageStatus.Pending:
                        theRow.DefaultCellStyle.BackColor = Color.Chartreuse;
                        break;
                    case PageStatus.Processing:
                        {
                            dgvListPage.FirstDisplayedScrollingRowIndex = rowIndex;
                            theRow.DefaultCellStyle.BackColor = Color.MediumBlue;
                            break;
                        }

                    case PageStatus.Success:
                        theRow.DefaultCellStyle.BackColor = Color.Aqua;
                        break;
                }

            }
        }
        #endregion

        #region gridView dvgListLink

        private void ListLinkRefresh()
        {
            dvgListLink.Refresh();
        }
        private void DvgListLinkInitialize()
        {
            dvgListLink.AutoGenerateColumns = false;
            dvgListLink.CellClick += dvgListLink_CellClick;
            _listLinkBindingSource = new BindingList<AnalysisLinkModel>();
            dvgListLink.DataSource = _listLinkBindingSource;
            dvgListLink.CellFormatting += dvgListLink_CellFormatting;
        }

        private void AddDataToListBindingSource(IList<AnalysisLinkModel> allLinkinPage)
        {
            _listLinkBindingSource.Clear();
            _listLinkBindingSource = new BindingList<AnalysisLinkModel>(allLinkinPage);
            dvgListLink.DataSource = _listLinkBindingSource;
            ListLinkRefresh();
        }
        private void dvgListLink_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not in our 
            var dataGridViewColumn = dvgListLink.Columns["btnDelete"];
            if (dataGridViewColumn == null || (e.ColumnIndex != dataGridViewColumn.Index || e.RowIndex < 0)) return;
            var rowIndex = e.RowIndex;
            _pageListBindingSource.RemoveAt(rowIndex);
            ListLinkRefresh();
        }

        private void dvgListLink_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            var colIndex = e.ColumnIndex;
            var rowIndex = e.RowIndex;

            if (rowIndex < 0 || colIndex < 0) return;
            var theRow = dvgListLink.Rows[rowIndex];
            var downloadCell = theRow.Cells["Downloaded"].Value.ToBooleanOrDefault(false);
            if (!downloadCell) return;
            theRow.DefaultCellStyle.BackColor = Color.YellowGreen;
            dvgListLink.FirstDisplayedScrollingRowIndex = rowIndex;
        }
        #endregion
    }
}
